/*
 * @lc app=leetcode.cn id=374 lang=rust
 *
 * [374] 猜数字大小
 */

// @lc code=start
/** 
 * Forward declaration of guess API.
 * @param  num   your guess
 * @return 	     -1 if num is lower than the guess number
 *			      1 if num is higher than the guess number
 *               otherwise return 0
 * unsafe fn guess(num: i32) -> i32 {}
 */

impl Solution {
    unsafe fn guessNumber(n: i32) -> i32 {
        let mut l = 1;
        let mut r = n;
        while l < r {
            let mid = l + (r - l) / 2;
            let flag = guess(mid);
            if flag == 0 {
                l = mid;
                break;
            }
            if flag == -1{
                r = mid - 1;//答案在[l, mid - 1]
            }
            else{
                l = mid + 1;//答案在[mid + 1, r]
            }
        }
        return l;
    }
}
// @lc code=end

